#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;

int n, q;
int a[N], ne[N], tt;
string s[N];

void solve()
{
    cin >> n >> q;
    while (q--) {
        int op, p;cin >> op >> p;
        if (op == 1) {
            a[p] = a[0];
        } else if (op == 2) {
            tt++;
            cin >> s[tt];
            ne[tt] = a[p];
            a[p] = tt;
        } else {
            a[0] = a[p];
        }
    }

    vector<string> segments;
    for (int i = a[0]; i != 0; i = ne[i]) {
        segments.push_back(s[i]);
    }

    string result;
    for (int i = segments.size() - 1; i >= 0; --i) {
        result += segments[i];
    }

    cout << result;
    }

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}